import QtQuick 2.8
import QtQuick.Controls 2.1
import QtQuick.Layouts 1.2

Page {
    id: pageTaskCounter
    title: qsTr("帐号")
    visible: true
 
    property StackView stack: null
    
    // property int currentIndex: 1
    property int currentIndex: rootWindow.batchIndex
    property int counterValue: 0

    function addIndex(){
        currentIndex ++;
        if(currentIndex > rootWindow.batchCount){
            currentIndex = rootWindow.batchCount;
        }
    }

    function minusIndex(){
        currentIndex --;
        if(currentIndex < 1){
            currentIndex = 1;
        }
    }

    function counterSubmit(next){
        if(counterValue <= 0){
            rootWindow.popupTips('无效的计数') //弹窗提示
            return;
        }

        rootWindow.popupShowLoading()
        var res = gHttp.countSubmit(rootWindow.posId, rootWindow.batchName, currentIndex, counterValue, 1);
        rootWindow.popupHideLoading()

        var json = JSON.parse(res);
        
        //提示后清空计数
        rootWindow.popupTips(json.message, 3000, function(){
            counterDisplay.text = '---';
            counterValue = 0;
            
            if(json.code >=0 && next){ //是否切换考场
                addIndex()
            }
        })
    }

    Keys.enabled: true
    focus: true
    Keys.onPressed: {

        if(event.key == Qt.Key_Escape) {
            stack.custPop()
            return;
        }
        if(event.key == Qt.Key_1){
            minusIndex()
        }
        if(event.key == Qt.Key_3){
            addIndex()
        }
        if(event.key == Qt.Key_5){
            counterSubmit(false)
        }
        if(event.key == Qt.Key_8){
            counterSubmit(true)
        }
        event.accepted = true
    }
    header: ToolBar {
        background: Rectangle {
            implicitWidth: 100
            implicitHeight: 40
            opacity: 1
            color: "#3f51b5"
        }
        RowLayout {
            anchors.fill: parent
            ToolButton {
                text: qsTr("‹返回[Esc]")
                onClicked: stack.custPop()
            }
            Label {
                text: "任务计数"
                font.pointSize: 16
                color: '#ffffff'
                elide: Label.ElideRight
                horizontalAlignment: Qt.AlignHCenter
                verticalAlignment: Qt.AlignVCenter
                Layout.fillWidth: true
            }
        }
    }
    footer: ToolBar {
        background: Rectangle {
            implicitWidth: 100
            implicitHeight: 40
            opacity: 1
            color: "#3f51b5"
        }
        RowLayout {
            anchors.fill: parent
            Label {
                text: "上一场[1]，下一场[3]， 确认提交[5]，确认提交并切下一场[8]"
                font.pointSize: 16
                color: '#ffffff'
                elide: Label.ElideRight
                horizontalAlignment: Qt.AlignHCenter
                verticalAlignment: Qt.AlignVCenter
                Layout.fillWidth: true
            }
        }
    }
    Column{
        anchors.fill: parent

        Rectangle{
            width: parent.width
            height: 30
            color: '#2c489c'
            Text{
                width: parent.width
                height: parent.height
                // horizontalAlignment: Text.AlignHCenter//(1)
                verticalAlignment: Text.AlignVCenter    //垂直居中，控件必须有height才可以使用
                horizontalAlignment: Text.AlignLeft
                font.pointSize: 20
                font.bold: true
                color: 'white'
                text:'考试名称：' + rootWindow.examName
            }
        }

        Rectangle{
            width: parent.width
            height: 30
            color: '#2c489c'
            Text{
                width: parent.width
                height: parent.height
                // horizontalAlignment: Text.AlignHCenter//(1)
                verticalAlignment: Text.AlignVCenter    //垂直居中，控件必须有height才可以使用
                horizontalAlignment: Text.AlignLeft
                font.pointSize: 20
                font.bold: true
                color: 'white'
                text:'考点名称：' + rootWindow.posName
            }
        }

        Rectangle{
            width: parent.width
            height: 30
            color: '#2c489c'
            Text{
                width: parent.width
                height: parent.height
                // horizontalAlignment: Text.AlignHCenter//(1)
                verticalAlignment: Text.AlignVCenter    //垂直居中，控件必须有height才可以使用
                horizontalAlignment: Text.AlignLeft
                font.pointSize: 20
                font.bold: true
                color: 'white'
                text: '批次：' + rootWindow.batchName + ', 考场总数：' + rootWindow.batchCount
            }
        }

        Rectangle{
            width: parent.width
            height: 80
            color: '#2c489c'
            Text{
                id: examIndex
                width: parent.width
                height: 80
                // horizontalAlignment: Text.AlignHCenter//(1)
                verticalAlignment: Text.AlignVCenter    //垂直居中，控件必须有height才可以使用
                horizontalAlignment: Text.AlignHCenter
                font.pointSize: 30
                font.bold: true
                color: '#91d145'
                text: `第：${currentIndex}考场`
            }
        }

        Rectangle{
            width: parent.width
            height: 80
            color: '#2c489c'
            Text{
                width: parent.width
                height: 80
                // horizontalAlignment: Text.AlignHCenter//(1)
                verticalAlignment: Text.AlignVCenter    //垂直居中，控件必须有height才可以使用
                horizontalAlignment: Text.AlignHCenter
                font.pointSize: 20
                font.bold: true
                color: '#91d145'
                text:'答题卡数量'
            }
        }

        Rectangle{
            width: parent.width
            color: '#2c489c'
            height: parent.height - 90
            Text{
                id: counterDisplay
                width: parent.width
                height: 90
                // horizontalAlignment: Text.AlignHCenter//(1)
                verticalAlignment: Text.AlignVCenter    //垂直居中，控件必须有height才可以使用
                horizontalAlignment: Text.AlignHCenter
                font.pointSize: 90
                font.bold: true
                color: '#91d145'
                text:'***'
            }
        }
    }
// 计数功能
    Connections{
        target: gCounter
        function onCounterNewValue(value){
            console.log(value)
            if(value <= 0){
                counterDisplay.text = '--';
            }else if(value == 30){ //自动提交
                //TODO 自动切换到下一考场
                counterDisplay.text = value;
                counterValue = value;
                counterSubmit(false)
            }else if (value < 18){
                rootWindow.popupTips('小于测量范围')
            }else if (value > 34){
                rootWindow.popupTips('大于测量范围')
            }else{
                counterDisplay.text = value;
                counterValue = value;
            }
        }
    }

    function beforeDestroy(){
        gCounter.counterStop()
    }
    
    Component.onCompleted:{
        console.log("输入的考场号为：" + rootWindow.batchIndex)
        // currentIndex = Number(pageTaskD.text) //https://doc.qt.io/qt-5/qml-qtqml-number.html#fromLocaleString-method
        // currentIndex = parseInt(pageTaskD.text) //https://www.cnblogs.com/linuxandmcu/p/11594541.html
        gCounter.counterStart()
    }
    Component.onDestruction:{
        console.log("onDestruction")
    }
}